package com.fruit.service.pay.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.fruit.mapper.pay.PayMapper;
import com.fruit.pojo.StockRecord.StockRecordGoods;
import com.fruit.pojo.depot.Depot;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.order.StockRecord;
import com.fruit.pojo.pay.TransactionBankFlow;
import com.fruit.pojo.pay.TransactionFlow;
import com.fruit.pojo.user.SysUser;
import com.fruit.service.pay.PayManager;
import com.fruit.service.pic.PictureManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DateUtil;

/** 支付
 * 修改时间：2016.07.15
 */
@Service("payService")
public class PayService implements PayManager{

	@Autowired
	private PayMapper payMapper;
	
	@Autowired
	private PictureManager pictureService;
	
	@Override
	public List<TransactionFlow> getTransactionFlowList(TransactionFlow t) {
		return payMapper.getTransactionFlowList(t);
	}

	@Override
	public int saveBankFlow(MultipartFile instrumen, TransactionBankFlow t,SysUser user) {
		int num=1;
		try {
			TransactionFlow tf=payMapper.getTransactionFlow(String.valueOf(t.getTransactionId()));
			if(tf == null){
				num=0;
				return num;
			}
			if("01".equals(tf.getState())){
				num=2;
				return num;
			}
			List<TransactionBankFlow> flow=	payMapper.getTransactionBankFlowList(String.valueOf(t.getTransactionId()));
			BigDecimal price=new BigDecimal("0.00");
			if(flow != null && flow.size()>0){
				for (TransactionBankFlow bank : flow) {
					price=price.add(bank.getPrice());
				}
			}
			BigDecimal tPrice=price.add(t.getPrice());
			if(tPrice.compareTo(tf.getTotalPrice())==1){
				num=3;
				return num;
			}
			Map<String, Object> imgResMap = pictureService.uploadPicture(instrumen.getInputStream(), "view", 800, 800, 0.8f);
			int error = (Integer)imgResMap.get("error");
			if(error != 0){
				num=0;
				return num;
			}
			String url = (String)imgResMap.get("url");
			t.setInstrument(url);
			if("consume".equals(tf.getType())){
				t.setPriceState("add");
			}else{
				t.setPriceState("sub");
			}
			t.setOperatorId(user.getUserId());
			t.setCreateTime(DateUtil.getTime());
			payMapper.saveBankFlow(t);
			 if(tf.getTotalPrice().compareTo(price.add(t.getPrice()))==0){
				 tf.setFinishTime(DateUtil.getTime());
				 TransactionFlow tt=new TransactionFlow();
				 tt.setId(tf.getId());
				 tt.setVersion(tf.getVersions());
				 tt.setCreateTime(DateUtil.getTime());
				 if(tf.getType().equals("consume")){
					 tt.setState("02");
					 num=payMapper.updateOrderState(tt);
					 if(num ==0){
						num=0;
						return num;
					 }
				 }else if(tf.getType().equals("refund")){
					 tt.setState("06");
					 num=payMapper.updateOrderState(tf);
					 if(num ==0){
						num=0;
						return num;
					 }
				 }
				num=payMapper.updateTransactionFlowState(tf);
				if(num ==0){
					num=0;
					return num;
				}
				Depot depot=payMapper.getDepot();
				if(depot ==null){
					num=0;
					return num;
				}
				StockRecord sr=new StockRecord();
				sr.setCodePre(DateUtil.getYear());
				sr.setCodeSuf(CustomUtil.codeSuf(3, tf.getUserId()));
				sr.setLinkmanName(depot.getLinkmanName());
				sr.setLinkmanPhone(depot.getLinkmanPhone());
				sr.setCreateTime(DateUtil.getTime());
				if(tf.getType().equals("consume")){
					sr.setType("SALES_OUT");
					sr.setDataType("OUT");
					sr.setObjId(tf.getOrderId());
				}else if(tf.getType().equals("refund")){
					sr.setType("SALES_RETURNS_IN");
					sr.setObjId(tf.getRefundId());
				}
				sr.setDepotId(depot.getId());
				sr.setOperatorId(String.valueOf(user.getUserId()));
				payMapper.addStockRecord(sr);
				List<Order> orderGoods =payMapper.getOrderGoods(tf);
				if(orderGoods.size()==0){
					num=0;
					return num;
				}
				for (Order order : orderGoods) {
					StockRecordGoods srg = new StockRecordGoods();
					srg.setSrId(sr.getId());
					srg.setGoodsId(order.getGoods_id());
					srg.setGoodsName(order.getTitle());
					srg.setSpecDesc(order.getSpecDesc());
					srg.setUnit(order.getUnit());
					srg.setStockUnit(order.getBuyUnit());
					srg.setNumber(order.getNumber());
					srg.setUnitNumber(order.getUnitNumber());
					srg.setCreateTime(DateUtil.getTime());
					payMapper.addStockRecordGoods(srg);
					srg.setType(tf.getType());
					/*num= payMapper.updateDepotStockNum(srg);
					if(num ==0){
						num=0;
						return num;
					}*/
					srg.setUpdateTime(DateUtil.getTime());
					srg.setUpdateId(user.getUserId());
					num=payMapper.updateGoodsNum(srg);
					if(num==0){
						num=0;
						return num;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			num=0;
			return num;
		}finally{
			if(num==0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		return num;
	}

	@Override
	public TransactionFlow getTransactionFlow(String id) {
		return payMapper.getTransactionFlow(id);
	}

	@Override
	public List<TransactionBankFlow> getTransactionBankFlowList(String id) {
		return payMapper.getTransactionBankFlowList(id);
	}

	
	
}
